package com.karl.mytask_2.tools;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.karl.mytask_2.R;

/**
 * Created by Karl on 2016/5/27.
 */
public class SelectPicPopupWindow extends PopupWindow {
    private Button btn_take_photo,btn_pick_photo,btn_cancel;
    private View mView;

    public SelectPicPopupWindow(Context context, View.OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = mInflater.inflate(R.layout.pop_layout,null);
        btn_take_photo = (Button) mView.findViewById(R.id.btn_take_photo);
        btn_pick_photo = (Button) mView.findViewById(R.id.btn_pick_photo);
        btn_cancel = (Button) mView.findViewById(R.id.btn_cancel);
        //取消按钮
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //设置按钮监听
        btn_take_photo.setOnClickListener(itemsOnClick);
        btn_pick_photo.setOnClickListener(itemsOnClick);
        //设置当前View
        this.setContentView(mView);
        //设置pop弹出窗体的宽度
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置pop弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置pop可点击
        this.setFocusable(true);
        //设置弹出窗体的动画效果
        this.setAnimationStyle(R.style.AppTheme);
        //实例化一个colorDrawable为半透明
        ColorDrawable cd = new ColorDrawable(0xb0000000);
        //设置pop的背景
        this.setBackgroundDrawable(cd);
        //添加点击事件：如果触摸屏幕在选择框外面则销毁
        mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = mView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP){
                    if (y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}
